using UnityEngine;
using System.Collections.Generic;

namespace TrainSystem
{
    public class TrainCarriage : MonoBehaviour
    {
        public Train locomotive;  // 火车头
        public TrainCarriage frontCarriage;  // 前一节车厢

        public Transform InitTransform;
        public float followDistance = 2f;  // 车厢间距

        private float currentSpeed;

        // 路径点相关
        public TrackPoint currentPoint;  // 当前轨道点
        public TrackPoint nextPoint;    // 下一个轨道点

        public GameObject Model;

        private void Start()
        {
            if (locomotive == null && frontCarriage == null)
            {
                Debug.LogError("Carriage needs either a locomotive or a front carriage!");
                return;
            }
        

            transform.position = InitTransform.position;
            transform.rotation = InitTransform.rotation;

            // UpdatePathPoints();
            currentPoint = GameEntry.Instance.trackPointManager.trackPoints1[0];
            nextPoint = GameEntry.Instance.trackPointManager.trackPoints1[1];
        }

        private void LateUpdate()
        {
            if (locomotive.nextPoint == null) return;
            // 更新路径点
            // UpdatePathPoints();

            // 更新速度和位置
            currentSpeed = GetCurrentSpeed();
        
            if (currentSpeed > 0 && nextPoint != null)
            {
                // 移动车厢
                float step = currentSpeed * Time.deltaTime;
                Vector3 targetPosition = nextPoint.transform.position;
                
                // 如果是第一节车厢，直接跟随火车头的路径点
                // if (frontCarriage == null)
                // {
                    transform.position = Vector3.MoveTowards(transform.position, targetPosition, step);
                    
                    // 更新当前点
                    if (Vector3.Distance(transform.position, targetPosition) < 0.1f)
                    {
                        currentPoint = nextPoint;
                        transform.position = currentPoint.transform.position;
                        UpdateNextPoint();
                    }
                // }
                // // 如果是其他车厢，需要保持间距
                // else
                // {
                //     // 计算与前车的实际距离
                //     float actualDistance = Vector3.Distance(transform.position, frontCarriage.transform.position);
                    
                //     if (actualDistance > followDistance)
                //     {
                //         // 如果距离过大，追赶前车
                //         Vector3 direction = (frontCarriage.transform.position - transform.position).normalized;
                //         transform.position += direction * step;
                //     }
                    
                //     // 更新路径点
                //     if (Vector3.Distance(transform.position, targetPosition) < 0.1f)
                //     {
                //         currentPoint = nextPoint;
                //         transform.position = currentPoint.transform.position;
                //         UpdateNextPoint();
                //     }
                // }

                // 更新旋转
                if (nextPoint != null)
                {
                    Vector3 direction = (nextPoint.transform.position - transform.position).normalized;
                    if (direction != Vector3.zero)
                    {
                        Quaternion targetRotation = Quaternion.LookRotation(direction);
                        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, step * 2);
                    }
                }
            }
        }

        private void UpdatePathPoints()
        {
            if (frontCarriage == null)
            {
                // 第一节车厢，跟随火车头
                if (locomotive != null)
                {
                    if (currentPoint == null)
                    {
                        currentPoint = locomotive.currentPoint;
                    }
                    nextPoint = locomotive.GetNextPoint();
                }
            }
            else
            {
                // 其他车厢，跟随前一节车厢
                if (currentPoint == null)
                {
                    currentPoint = frontCarriage.currentPoint;
                }
                nextPoint = frontCarriage.nextPoint;
            }
        }

        private void UpdateNextPoint()
        {
            if (frontCarriage == null && locomotive != null)
            {
                nextPoint = locomotive.GetNextPoint();
            }
            else if (frontCarriage != null)
            {
                nextPoint = frontCarriage.nextPoint;
            }
        }

        public float GetCurrentSpeed()
        {
            if (frontCarriage != null)
            {
                return frontCarriage.GetCurrentSpeed();
            }
            return locomotive.currentSpeed;
        }

        public void OpenModel()
        {
            Model.gameObject.SetActive(true);
        }
    }
}